.TH std::basic_istringstream::basic_istringstream 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::basic_istringstream::basic_istringstream \- std::basic_istringstream::basic_istringstream

.SH Synopsis
   explicit basic_istringstream( std::ios_base::openmode mode =           \fI(until C++11)\fP
                                     std::ios_base::in );
   explicit basic_istringstream( std::ios_base::openmode mode );          \fI(since C++11)\fP
   basic_istringstream()                                             \fB(2)\fP  \fI(since C++11)\fP
       : basic_istringstream(std::ios_base::in) {}
   explicit basic_istringstream

       ( const std::basic_string<CharT, Traits, Allocator>& str,     \fB(3)\fP
         std::ios_base::openmode mode =

             std::ios_base::in );
   explicit basic_istringstream

       ( std::basic_string<CharT, Traits, Allocator>&& str,          \fB(4)\fP  \fI(since C++20)\fP
         std::ios_base::openmode mode =

             std::ios_base::in );
   basic_istringstream( std::ios_base::openmode mode, const          \fB(5)\fP  \fI(since C++20)\fP
   Allocator& a );
   template< class SAlloc >

   basic_istringstream( const std::basic_string<CharT, Traits,
   SAlloc>& str,                                                     \fB(6)\fP  \fI(since C++20)\fP

                        std::ios_base::openmode mode, const
   Allocator& a );
   template< class SAlloc >

   basic_istringstream( const std::basic_string<CharT, Traits,   \fB(1)\fP
   SAlloc>& str,                                                     \fB(7)\fP  \fI(since C++20)\fP
                        const Allocator& a )

       : basic_istringstream(str, std::ios_base::in, a) {}
   template< class SAlloc >

   explicit basic_istringstream
       ( const std::basic_string<CharT, Traits, SAlloc>& str,        \fB(8)\fP  \fI(since C++20)\fP
         std::ios_base::openmode mode =

             std::ios_base::in );
   template< class StringViewLike >

   explicit basic_istringstream
       ( const StringViewLike& t,                                    \fB(9)\fP  (since C++26)
         std::ios_base::openmode mode =

             std::ios_base::in );
   template< class StringViewLike >

   basic_istringstream( const StringViewLike& t,                     \fB(10)\fP (since C++26)

                        std::ios_base::openmode mode, const
   Allocator& a );
   template< class StringViewLike >
   basic_istringstream( const StringViewLike& t, const               \fB(11)\fP (since C++26)
   Allocator& a );
   basic_istringstream( basic_istringstream&& other );               \fB(12)\fP \fI(since C++11)\fP

   Constructs new string stream.

   Given

     * base_type as std::basic_istream<CharT, Traits>, and
     * buf_type as std::basic_stringbuf<CharT, Traits, Allocator>,

   the std::basic_istream base and the exposition-only data member sb are initialized
   as follows.

    Over           std::basic_istream base                           sb
    load
   \fB(1)\fP                                               buf_type(mode | std::ios_base::in)
   \fB(2)\fP                                               buf_type(std::ios_base::in)
   \fB(3)\fP                                               buf_type(str, mode |
                                                     std::ios_base::in)
   \fB(4)\fP                                               buf_type(std::move(str), mode |
                                                     std::ios_base::in)
   \fB(5)\fP    base_type(std::addressof(sb))^[1]          buf_type(mode | std::ios_base::in,
                                                     a)
   \fB(6)\fP                                               buf_type(str, mode |
                                                     std::ios_base::in, a)
   \fB(7)\fP                                               buf_type(str, std::ios_base::in,
                                                     a)
   \fB(8)\fP                                               buf_type(str, mode |
                                                     std::ios_base::in)
   \fB(9)\fP                                               {t, mode | std::ios_base::in,
          std::addressof(sb)                         Allocator()}
   \fB(10)\fP                                              {t, mode | std::ios_base::in, a}
   \fB(11)\fP                                              {t, std::ios_base::in, a}
   \fB(12)\fP   move constructed from other's              move constructed from other.sb
          std::basic_istream base

    1. ↑ The std::basic_iostream base was intialized with base_type(&sb) (for overloads
       (1,3)) until C++11.
   8) This overload participates in overload resolution only if std::is_same_v<SAlloc,
   Allocator> is false.
   9-11) These overloads participate in overload resolution only if
   std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>
   is true.

.SH Parameters

   str   - string to use as initial contents of the string stream
   t     - an object (convertible to std::basic_string_view) to use as initial contents
           of the string stream
   a     - allocator used for allocating the contents of the string stream
           specifies stream open mode. It is a BitmaskType, the following constants are
           defined:

           Constant          Explanation
           app               seek to the end of stream before each write
   mode  - binary            open in binary mode
           in                open for reading
           out               open for writing
           trunc             discard the contents of the stream when opening
           ate               seek to the end of stream immediately after open
           noreplace (C++23) open in exclusive mode
   other - another string stream to use as source

.SH Notes

   Construction of one-off basic_istringstream objects in a tight loop, such as when
   used for string conversion, may be significantly more costly than calling str() to
   reuse the same object.

           Feature-test macro          Value    Std                Feature
   __cpp_lib_sstream_from_string_view 202306L (C++26) Interfacing std::stringstreams
                                                      with std::string_view, (9-11)

.SH Example


// Run this code

 #include <iostream>
 #include <sstream>

 int main()
 {
     // default constructor (input/output stream)
     std::stringstream buf1;
     buf1 << 7;
     int n = 0;
     buf1 >> n;
     std::cout << "buf1 = " << buf1.str() << " n = " << n << '\\n';

     // input stream
     std::istringstream inbuf("-10");
     inbuf >> n;
     std::cout << "n = " << n << '\\n';

     // output stream in append mode \fI(C++11)\fP
     std::ostringstream buf2("test", std::ios_base::ate);
     buf2 << '1';
     std::cout << buf2.str() << '\\n';
 }

.SH Output:

 buf1 = 7 n = 7
 n = -10
 test1

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

     DR    Applied to        Behavior as published         Correct behavior
   P0935R0 C++11      the default constructor was explicit made implicit

.SH See also

   str           gets or sets the contents of underlying string device object
                 \fI(public member function)\fP
                 constructs a basic_stringbuf object
   constructor   \fI\fI(public member\fP function of\fP
                 std::basic_stringbuf<CharT,Traits,Allocator>)
